package com.zjq.springsocial.renren.api.impl.json;

import java.io.IOException;

import org.codehaus.jackson.JsonParser;
import org.codehaus.jackson.JsonProcessingException;
import org.codehaus.jackson.annotate.JsonCreator;
import org.codehaus.jackson.annotate.JsonIgnoreProperties;
import org.codehaus.jackson.annotate.JsonProperty;
import org.codehaus.jackson.map.DeserializationContext;
import org.codehaus.jackson.map.JsonDeserializer;
import org.codehaus.jackson.map.annotate.JsonDeserialize;

import com.zjq.springsocial.renren.api.School.EducationBackground;

@JsonIgnoreProperties(ignoreUnknown = true)
abstract class SchoolMixin {

	@JsonCreator
	SchoolMixin(
			@JsonProperty("name") String name,//学校名称
			@JsonProperty("year") String year,//入学日期
			@JsonProperty("department") String department,//所在专业
			@JsonProperty("educationBackground") 
			@JsonDeserialize(using = EducationBackgroundDeserializer.class)
			EducationBackground educationBackground //学历
	){}
	
	private static class EducationBackgroundDeserializer extends JsonDeserializer<EducationBackground> {
		@Override
		public EducationBackground deserialize(JsonParser jp, DeserializationContext ctxt)
				throws IOException, JsonProcessingException {
			try {
				return EducationBackground.valueOf(jp.getText().toUpperCase());
			} catch (IllegalArgumentException e) {
				return EducationBackground.SECRET;
			}
		}
	}
}
